using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class UIManager:MonoBehaviour {
    private static UIManager _instance;
    public static UIManager Instance { 
        get {
            if (_instance == null) {
                GameObject _go = new GameObject("UIManager");
                _instance = _go.AddComponent<UIManager>();
                DontDestroyOnLoad(_go);
            }
            return _instance; 
        }
    }

    public T ShowPanel<T>() where T : BasePanel {
        
        T _panel = GetPanel<T>();
        _panel.Show<T>();
        return _panel;
    }

    public T ClosePanel<T>() where T : BasePanel {
        T panel = GetPanel<T>();
        
        if (panel != null){
            panel.Close();
        }
        return panel;
    }

    public void DestoryPanel<T>() where T : BasePanel {
        GameObject _activeObj = BasePanel.GetPanel<T>();
        if (_activeObj != null) {
            Destroy(_activeObj);
        }
    }

    T GetPanel<T>() where T : BasePanel  {
        GameObject _activeObj =  BasePanel.GetPanel<T>();
        if (_activeObj == null) {
            T _panel = BasePanel.GetPrefab<T>();
            GameObject _obj = _panel.LoadPanel<T>();
            return _obj.GetComponent<T>();
        }
        
        
        return _activeObj.GetComponent<T>();
    }
    
}
